#!/bin/bash

# 设置颜色
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 获取版本号
VERSION=${1:-latest}

echo -e "${GREEN}=== LikeAdmin Python 生产环境部署脚本 ===${NC}"
echo -e "${BLUE}版本号: ${VERSION}${NC}"

# 检查Docker是否安装
if ! command -v docker &> /dev/null; then
    echo -e "${RED}错误: Docker 未安装${NC}"
    exit 1
fi

# 检查Docker Compose是否安装
if ! command -v docker-compose &> /dev/null; then
    echo -e "${RED}错误: Docker Compose 未安装${NC}"
    exit 1
fi

# 提示配置Docker镜像源
echo -e "${YELLOW}提示: 为加快下载速度，建议配置Docker镜像源${NC}"
echo -e "${BLUE}配置文件位置:${NC}"
echo -e "  Linux: /etc/docker/daemon.json"
echo -e "  Windows: %programdata%\\docker\\config\\daemon.json"
echo -e "  macOS: ~/.docker/daemon.json"
echo -e "${BLUE}参考配置:${NC}"
echo -e "  cp daemon.json /etc/docker/daemon.json"
echo -e "  systemctl restart docker"
echo ""

# 检查环境变量文件
if [ ! -f ".env" ]; then
    echo -e "${YELLOW}创建生产环境配置文件...${NC}"
    cat > .env << EOF
# 数据库配置
MYSQL_ROOT_PASSWORD=your_secure_password
MYSQL_DATABASE=likeadmin
MYSQL_USER=likeadmin
MYSQL_PASSWORD=your_secure_password
MYSQL_PORT=3306

# Redis配置
REDIS_PASSWORD=your_redis_password
REDIS_PORT=6379

# 应用配置
VERSION=${VERSION}
DOMAIN=https://your-domain.com
SECRET=your_secret_key

# 端口配置
ADMIN_API_PORT=8000
FRONT_API_PORT=8002
ADMIN_UI_PORT=5173
NGINX_HTTP_PORT=80
NGINX_HTTPS_PORT=443
EOF
    echo -e "${GREEN}配置文件已创建: .env${NC}"
    echo -e "${YELLOW}请编辑 .env 文件，配置正确的参数${NC}"
    exit 1
fi

# 加载环境变量
echo -e "${YELLOW}加载环境变量...${NC}"
export $(cat .env | grep -v '^#' | xargs)

# 创建必要的目录
echo -e "${YELLOW}创建必要的目录...${NC}"
mkdir -p data/mysql5.7.29/lib
mkdir -p data/redis
mkdir -p data/uploads
mkdir -p ssl
mkdir -p config/mysql

# 复制MySQL配置
if [ ! -f "config/mysql/mysqld.cnf" ]; then
    echo -e "${YELLOW}复制MySQL配置文件...${NC}"
    cp ../docker/config/mysql/mysqld.cnf config/mysql/
fi

# 检查SSL证书
if [ ! -f "ssl/cert.pem" ] || [ ! -f "ssl/key.pem" ]; then
    echo -e "${YELLOW}生成自签名SSL证书...${NC}"
    openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
        -keyout ssl/key.pem -out ssl/cert.pem \
        -subj "/C=CN/ST=State/L=City/O=Organization/CN=localhost"
    echo -e "${GREEN}SSL证书已生成${NC}"
fi

# 检查镜像是否存在
FASTAPI_IMAGE="likeadmin-python:fastapi-${VERSION}"
ADMIN_IMAGE="likeadmin-python:admin-${VERSION}"
NGINX_IMAGE="likeadmin-python:nginx-${VERSION}"

echo -e "${YELLOW}检查镜像...${NC}"
if ! docker images | grep -q "likeadmin-python:fastapi-${VERSION}"; then
    echo -e "${RED}错误: FastAPI 镜像不存在，请先运行构建脚本${NC}"
    echo -e "${BLUE}运行命令: ./build-images.sh ${VERSION}${NC}"
    exit 1
fi

if ! docker images | grep -q "likeadmin-python:admin-${VERSION}"; then
    echo -e "${RED}错误: Admin UI 镜像不存在，请先运行构建脚本${NC}"
    echo -e "${BLUE}运行命令: ./build-images.sh ${VERSION}${NC}"
    exit 1
fi

if ! docker images | grep -q "likeadmin-python:nginx-${VERSION}"; then
    echo -e "${RED}错误: Nginx 镜像不存在，请先运行构建脚本${NC}"
    echo -e "${BLUE}运行命令: ./build-images.sh ${VERSION}${NC}"
    exit 1
fi

# 停止现有服务
echo -e "${YELLOW}停止现有服务...${NC}"
docker-compose down

# 启动服务
echo -e "${YELLOW}启动生产服务...${NC}"
docker-compose up -d

# 等待服务启动
echo -e "${YELLOW}等待服务启动...${NC}"
sleep 60

# 检查服务状态
echo -e "${YELLOW}检查服务状态...${NC}"
docker-compose ps

# 检查服务健康状态
echo -e "${YELLOW}检查服务健康状态...${NC}"
for service in mysql redis fastapi-admin fastapi-front admin-ui nginx; do
    if docker-compose ps | grep -q "${service}.*Up"; then
        echo -e "${GREEN}✓ ${service} 运行正常${NC}"
    else
        echo -e "${RED}✗ ${service} 运行异常${NC}"
    fi
done

# 显示访问信息
echo -e "${GREEN}=== 部署完成 ===${NC}"
echo -e "${GREEN}管理后台: ${DOMAIN}${NC}"
echo -e "${GREEN}管理后台API: ${DOMAIN}/api/admin${NC}"
echo -e "${GREEN}前台API: ${DOMAIN}/api/front${NC}"
echo -e "${GREEN}MySQL: localhost:${MYSQL_PORT}${NC}"
echo -e "${GREEN}Redis: localhost:${REDIS_PORT}${NC}"

echo -e "${YELLOW}=== 常用命令 ===${NC}"
echo -e "${BLUE}查看日志: docker-compose logs -f${NC}"
echo -e "${BLUE}停止服务: docker-compose down${NC}"
echo -e "${BLUE}重启服务: docker-compose restart${NC}"
echo -e "${BLUE}更新服务: docker-compose pull && docker-compose up -d${NC}" 